package com.uday.myworld;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class add extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        
        Button btn1 = (Button) findViewById(R.id.Asubmit);
        btn1.setOnClickListener(listener_login);
    }
    private OnClickListener listener_login = new OnClickListener() {

        boolean check = false;

        public void onClick(View v) {
        	Context context = getApplicationContext();
            EditText locationText = (EditText) findViewById(R.id.Aloc);
            EditText infoText = (EditText) findViewById(R.id.Ainfo);
            EditText addressText = (EditText) findViewById(R.id.Aaddress);

            String location = locationText.getText().toString();
            String address = (addressText.getText().toString());
            String information = (infoText.getText().toString());
            

           
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("location", location));
            nameValuePairs.add(new BasicNameValuePair("address", address));
            nameValuePairs.add(new BasicNameValuePair("information", information));

            
                      
            try {
            	  HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.10.10.121:8084/Myworld/insert.jsp");
            	  //HttpPost httppost = new HttpPost(R.string.url+"insert.jsp");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                
                HttpEntity entity = response.getEntity();                
                InputStream is = entity.getContent();
                BufferedReader in = new BufferedReader(new InputStreamReader(is));
            	String inputLine,json="";
            	while ((inputLine = in.readLine()) != null) {
            		json=json+inputLine;
            	}		
            	in.close();
              
                JSONArray json1=new JSONArray(json);
        		String retrived=(String) json1.get(0);
        			//Toast.makeText(context, retrived, Toast.LENGTH_LONG).show();
              
                
               
        		if(retrived.equals("Inserted"))
                {
                     check=true;
                     Toast.makeText(context, "added sucessfully", Toast.LENGTH_SHORT).show();
                }
                else
                {
                	Toast.makeText(context, "not added", Toast.LENGTH_LONG).show();
                }
           
    	        if(check)
    	        {
    	            Intent myintent = new Intent(add.this, first.class);
    	            startActivity(myintent);
    	        }    		
       
                
            } catch (Exception e) {
                TextView tv = (TextView) findViewById(R.id.err);
                tv.setText("Error parsing data " + e.toString());
                Log.d("Error parsing data ",e.toString());
            }
            
            
            
           
              


        }
    };
        
        
        
    @Override
  	public boolean onCreateOptionsMenu(Menu menu) {
  		MenuInflater inflater = getMenuInflater();
  		inflater.inflate(R.layout.menu, menu);
  		return true;
  	}

  	@Override
  	public boolean onOptionsItemSelected(MenuItem item) {
  		switch (item.getItemId()) {
  		case R.id.exit:
  			finishActivity(1);
  			return true;
  		default:
  			return super.onOptionsItemSelected(item);
  		}
  	}  
    
	

	
}
